package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.FirstMatchBean;
import cn.bocweb.spinning.model.Bean.FirstRewardBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

public class TestRuleActivity extends BaseActivity {

    String url;
    UserPresenter mUserPresenter;
    LoadingDialog mLoadingDialog;
    boolean isFirst;
    UserPresenter mPresenter;
    UserBean.DataEntity dataEntity;

    @Bind(R.id.test_rule)
    WebView mTestRule;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @BindColor(R.color.white)
    int white;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_rule);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        mUserPresenter = new UserPresenterImp();
        mLoadingDialog = new LoadingDialog(this);
        mPresenter = new UserPresenterImp();
        dataEntity = new UserBean.DataEntity();
        initToolbar();
        initIntent();
        initWebView();

        mLoadingDialog.show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mTestRule.loadUrl(url);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Constant.REWARD_TO_LOGIN: {
                if (resultCode == Constant.REWARD_TO_LOGIN) {
                    if (mLoadingDialog == null) {
                        mLoadingDialog = new LoadingDialog(this);
                        mLoadingDialog.show();
                    } else {
                        mLoadingDialog.show();
                    }
                    UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                    dataEntity.readInfoFromLocal(TestRuleActivity.this);
                    mPresenter.getFirstReward(dataEntity.getUserid());
                }
                break;
            }
            case Constant.TEST_RULE_BACK_LOGIN: {
                break;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    private void initWebView() {

        WebSettings settings = mTestRule.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mTestRule.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                dialog.dismiss();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("http://match")) {
                    dataEntity.readInfoFromLocal(TestRuleActivity.this);
                    if (TextUtils.isEmpty(dataEntity.getUserid())) {
                        Intent intent = new Intent(TestRuleActivity.this, LoginAcivity.class);
                        intent.putExtra(Constant.ACTIVITY_TO_LOGIN, 101);
                        startActivity(intent);
                    } else {
                        mUserPresenter.getFirstMatch();
                    }
                    return true;
                } else if (url.contains("http://rank/15")) {
                    Intent intent = new Intent(TestRuleActivity.this, RankActivity.class);
                    intent.putExtra(Constant.TO_RANK_LIBID, url.substring(url.length() - 2));
                    intent.putExtra(Constant.TO_TEST_RANK_IF_FIRST, true);
                    startActivity(intent);
                    return true;
                } else if (url.contains("http://hongbao")) {
                    dataEntity.readInfoFromLocal(TestRuleActivity.this);
                    if (TextUtils.isEmpty(dataEntity.getUserid())) {
                        Intent intent = new Intent(TestRuleActivity.this, LoginAcivity.class);
                        intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.REWARD_TO_LOGIN);
                        startActivityForResult(intent, Constant.REWARD_TO_LOGIN);
                    } else {
                        mPresenter.getFirstReward(dataEntity.getUserid());
                    }
                    return true;
                }
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mLoadingDialog.dismiss();
            }
        });
    }

    public void onEventMainThread(FirstRewardBean bean) {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        Intent intent = new Intent(this, FirstRewardActivity.class);
        intent.putExtra(Constant.TO_FIRST_REWARD, bean.getUrl());
        intent.putExtra(Constant.TO_TEST_RANK_IF_FIRST, true);
        startActivity(intent);
    }

    public void onEventMainThread(FirstMatchBean bean) {
        if (bean.getReturn_code() == 0) {
            Intent intent = new Intent(TestRuleActivity.this, ReadyTestActivity.class);
            intent.putExtra(Constant.TO_READY_FIRST_TEST_ID, bean.getId());
            intent.putExtra(Constant.TO_TEST_RANK_IF_FIRST, true);
            startActivity(intent);
            isFirst = true;
//            finish();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }
    }

    private void initIntent() {
        Intent intent = getIntent();
        url = intent.getStringExtra(Constant.TO_TEST_RULE);
        if (!TextUtils.isEmpty(url)) {
            mTestRule.loadUrl(url);
        }
    }

    private void initToolbar() {
        mToolbar.setTitle("\"金轮-蓝钻杯\"全国梳理知识大赛");
        mToolbar.setTitleTextColor(white);
        mToolbar.setNavigationIcon(R.mipmap.btn_exit);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
